package com.saic.data.util;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

/**
 * @DESC:CSV解析
 * @谈晟
 */
public class CsvParser {

	public static List<HashMap<String, String>> parserCvs(String _csvContent,
			boolean _ignoreFault) throws Exception {
		try {
			return CsvParser.Csv2Map(new StringReader(_csvContent),
					_ignoreFault);
		} catch (ArrayIndexOutOfBoundsException e) {
			throw new ArrayIndexOutOfBoundsException(e.getMessage());
		}
	}

	/**
	 * @DESC:解析CSV文件，带表头
	 * @param _reader
	 * @param _ignoreFault
	 * @return
	 * @throws Exception
	 */
	public static List<HashMap<String, String>> Csv2Map(Reader _reader,
			boolean _ignoreFault) throws Exception {
		List<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
		CSVReader reader = new CSVReader(_reader);
		List<String[]> fileEntiries = reader.readAll();
		if (fileEntiries != null && !fileEntiries.isEmpty()) {
			String[] header = fileEntiries.get(0);
			int rowCount = fileEntiries.size() - 1;
			int cellCount = header.length;
			System.out.println("rowCount: " + rowCount);
			System.out.println("cellCount: " + cellCount);

			L1: for (int i = 0; i < rowCount; i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				String[] line = fileEntiries.get(i + 1);
				for (int j = 0; j < cellCount; j++) {
					try {
						if (line.length == 1 && line[0].trim().equals(""))
							continue L1;
						map.put(header[j], line[j]);
					} catch (ArrayIndexOutOfBoundsException e) {
						if (!_ignoreFault) {
							reader.close();
							List<String> wrongLine = new ArrayList<String>();
							Collections.addAll(wrongLine, line);
							throw new ArrayIndexOutOfBoundsException(
									"cell count doesn't match, row: " + j
											+ ", " + wrongLine);
						}
					}

				}
				result.add(map);
			}
		}
		reader.close();
		return result;
	}

	/**
	 * @DESC:解析CSV/TXT文件，从第一行开始读取，自定义分隔符
	 * @param _reader
	 * @param _ignoreFault
	 * @return
	 * @throws Exception
	 */
	public static List<HashMap<String, String>> Csv2Map_(Reader _reader,
			boolean _ignoreFault, char token) throws Exception {
		List<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
		CSVReader reader = new CSVReader(_reader, token);
		List<String[]> fileEntiries = reader.readAll();
		if (fileEntiries != null && !fileEntiries.isEmpty()) {
			int rowCount = fileEntiries.size();
			int cellCount = fileEntiries.get(0).length;

			L1: for (int i = 0; i < rowCount; i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				String[] line = fileEntiries.get(i);
				for (int j = 0; j < cellCount; j++) {
					try {
						if (line.length == 1 && line[0].trim().equals(""))
							continue L1;
						map.put("col" + j, line[j]);
					} catch (ArrayIndexOutOfBoundsException e) {
						if (!_ignoreFault) {
							reader.close();
							List<String> wrongLine = new ArrayList<String>();
							Collections.addAll(wrongLine, line);
							throw new ArrayIndexOutOfBoundsException(
									"cell count doesn't match, row: " + j
											+ ", " + wrongLine);
						}
					}

				}
				result.add(map);
			}
		}
		reader.close();
		return result;
	}

	public static List<HashMap<String, String>> parserCvs(File _csvFile,
			boolean _ignoreFault) throws Exception {
		try {
			return CsvParser.Csv2Map(new FileReader(_csvFile), _ignoreFault);
		} catch (ArrayIndexOutOfBoundsException e) {
			throw new ArrayIndexOutOfBoundsException(e.getMessage()
					+ ", in file: " + _csvFile.getName());
		}
	}
}
